package com.yixun.qfbao.hander;

import com.yixun.common.page.Page;
import com.yixun.qfbao.api.service.CompanyService;
import com.yixun.qfbao.apidto.SmsReportPageReqDto;
import com.yixun.qfbao.apidto.SmsReportPageResDto;
import com.yixun.qfbao.dto.base.BaseRequestDTO;
import com.yixun.qfbao.dto.base.BaseResponseDTO;
import com.yixun.qfbao.hander.base.CommonAbstractHander;
import com.yixun.qfbao.model.Company;
import com.yixun.qfbao.model.PartnerInfo;
import com.yixun.qfbao.model.SmsHistory;
import com.yixun.qfbao.model.dto.SmsHistoryDto;
import com.yixun.qfbao.model.enums.SendStatusEnum;
import com.yixun.qfbao.service.PartnerInfoService;
import com.yixun.qfbao.service.SmsHistoryService;
import com.yixun.qfbao.vo.SmsReportPageReqVo;
import com.yixun.qfbao.xf.model.enums.ResultEnum;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class SmsReportExportHander extends CommonAbstractHander {
    private static Logger logger= LoggerFactory.getLogger(SmsReportExportHander.class);

    @Autowired
    private CompanyService companyService;
    @Autowired
    private SmsHistoryService smsHistoryService;
    @Autowired
    private PartnerInfoService partnerInfoService;
    @Override
    public BaseResponseDTO doBusiness(BaseRequestDTO reqDto) {
        SmsReportPageReqVo req = (SmsReportPageReqVo)reqDto;
        SmsReportPageReqDto reqDto1 = req.getParams();
        BaseResponseDTO<SmsReportPageResDto> res = new BaseResponseDTO<SmsReportPageResDto>();
        SmsReportPageResDto resDto = new SmsReportPageResDto();

        try{
            PartnerInfo p = partnerInfoService.selectById(Long.parseLong(reqDto1.getUserId()));

            Company company = companyService.getCompanyById(p.getHigherid());
            if (null != company) {
                resDto.setCompanyName(company.getCompanyName());
            }

            List<SmsHistory> smsHisList = new ArrayList<SmsHistory>();
            SmsHistoryDto smsHisDto = new SmsHistoryDto();
            smsHisDto.setMerchantId(company.getId());
            smsHisDto.setCreateTimeBefore(reqDto1.getBeginDate());
            smsHisDto.setCreateTimeAfter(reqDto1.getEndDate());
            smsHisDto.setSendStatus(SendStatusEnum.SUCCESS.getNo());
            smsHisList = smsHistoryService.selectListByDto(smsHisDto);
            Page<SmsHistory> page = new Page<SmsHistory>(smsHisList.size(), smsHisList);
            resDto.setPage(page);

        }catch(Exception e){
            logger.error("SmsReportExportHander error: {}", e);
            resDto.setResult(ResultEnum.FAIL.getNo());
            resDto.setResultMsg("系统异常");
        }
        res.setResData(resDto);
        res.setResult(resDto.getResult());
        return res;
    }
}
